/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFRelatorio.java
 *
 * Created on 06/12/2011, 20:16:06
 */

package calibracaolion;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Santi
 */
public class JFRelatorio extends javax.swing.JFrame {

    /** Creates new form JFRelatorio */
    public JFRelatorio() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(calibracaolion.CalibracaoLionApp.class).getContext().getResourceMap(JFRelatorio.class);
        entityManager1 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory(resourceMap.getString("entityManager1.persistenceUnit")).createEntityManager(); // NOI18N
        query1 = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery(resourceMap.getString("query1.query")); // NOI18N
        list1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query1.getResultList());
        jLabel1 = new javax.swing.JLabel();
        tag = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        tag.setText(resourceMap.getString("tag.text")); // NOI18N
        tag.setName("tag"); // NOI18N
        tag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tagActionPerformed(evt);
            }
        });

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(105, 105, 105)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                            .addComponent(tag, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                        .addGap(124, 124, 124))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, tag});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tag, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(119, 119, 119))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tagActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tagActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
            /* Preenche o relatório com os dados. Gera o arquivo .jrprint */
            //Pedidos oPedido = new Pedidos();

                String caminhoRelJasper = "resources/rCalibracao.jasper";
                ResultSet rs = null;

            try {
                Calibracao oCalibracao = new Calibracao();
                Connection jpa = ((oracle.toplink.essentials.ejb.cmp3.EntityManager) entityManager1).getServerSession().getDefaultConnectionPool().acquireConnection().getConnection();
                
                rs = oCalibracao.getImprimir(jpa,tag.getText());
                JRResultSetDataSource ds = new JRResultSetDataSource(rs);
                Map parametros = new HashMap();
                parametros.put("tagParameter",tag.getText());
                URL arquivo = getClass().getResource(caminhoRelJasper);
                if (arquivo == null)
                JOptionPane.showMessageDialog(null,"Arquivo jasper não encontrado");
                JasperReport report;
            try {

                    report = JasperManager.loadReport(arquivo.openStream());
                    JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap(), ds);
                    JasperViewer jrviewer = new JasperViewer(jasperPrint, false);
                    jrviewer.setVisible(true);


            } catch (JRException ex) {
                JOptionPane.showMessageDialog(null, ex, "ATENÇÃO", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(JFRelatorio.class.getName()).log(Level.SEVERE, null, ex);
            }

            } catch (SQLException ex) {
            Logger.getLogger(JFRelatorio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex, "ATENÇÃO", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(JFRelatorio.class.getName()).log(Level.SEVERE, null, ex);
            }


            
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFRelatorio().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager entityManager1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private java.util.List list1;
    private javax.persistence.Query query1;
    private javax.swing.JTextField tag;
    // End of variables declaration//GEN-END:variables

}
